package com.ysyc.driver_ee.widget;/**
 * Created by Administrator on 2017/4/12.
 */

import android.content.Context;
import android.support.annotation.ColorRes;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ysyc.driver_ee.R;
import com.ysyc.driver_ee.tools.ScreenUtils;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * App公用头部布局
 */
public class TitleView extends LinearLayout {
    @BindView(R.id.backview_img)
    ImageButton backviewImg;
    @BindView(R.id.backView_txt)
    TextView backViewTxt;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.rightview_img)
    ImageButton rightviewImg;
    @BindView(R.id.right_txt)
    TextView rightTxt;
    @BindView(R.id.topView_layout)
    RelativeLayout topViewLayout;
    private View view;


    public TitleView(Context context) {
        super(context);
        initView(context);
    }

    private void initView(Context context) {
        view = LayoutInflater.from(context).inflate(R.layout.title_layout, this);
        ButterKnife.bind(view);

        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                int stutasHeight = ScreenUtils.getStatusHeight(getContext());
                topViewLayout.getLayoutParams().height = getResources().getDimensionPixelSize(R.dimen.topview_height) + stutasHeight;


                for (int i = 0; i < topViewLayout.getChildCount(); i++) {
                    View view = topViewLayout.getChildAt(i);
                    view.setPadding(0, stutasHeight, 0, 0);
                }
            }
        });
    }

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public void setTopViewHeightWithStuatsBar() {
        int stutasHeight = ScreenUtils.getStatusHeight(getContext());

        topViewLayout.getLayoutParams().height = getResources().getDimensionPixelSize(R.dimen.topview_height)+stutasHeight;
        titleContent.setPadding(0,stutasHeight,0,0);
    }

    public void setTopViewHeight(int height) {
        topViewLayout.getLayoutParams().height = height;
    }

    /**
     * 隐藏所有的控件
     */
    public void hiddenAllCompent() {
        for(int i=0; i<topViewLayout.getChildCount(); i++) {
            topViewLayout.getChildAt(i).setVisibility(View.INVISIBLE);
        }
    }

    public void setTitleViewBackground(@ColorRes int color) {
        topViewLayout.setBackgroundResource(color);
    }

    /**
     * 设置中间的文字
     *
     * @param
     */
    public void setTitle(String text) {
        if (titleContent != null) {
            titleContent.setVisibility(View.VISIBLE);
            titleContent.setText(text);
        }
    }

    /**
     * 设置中间的颜色
     *
     * @param
     */
    public void setTitle(String text, int color) {
        if (titleContent != null) {
            titleContent.setVisibility(View.VISIBLE);
            titleContent.setText(text);
            titleContent.setTextColor(color);
        }
    }
    /**
     * @param text
     * 设置中间文字斜体加粗
     */
    public void setTitleWithBoldAndItalic(String text){
        if (titleContent != null) {
            titleContent.setVisibility(View.VISIBLE);
            titleContent.getPaint().setFakeBoldText(true);
            titleContent.getPaint().setTextSkewX(-0.3f);
            titleContent.setText(text);
        }
    }

    /**
     * 设置左边的文字
     *
     * @param text
     */
    public void setLeftText(String text) {
        if (backViewTxt != null) {
            backViewTxt.setVisibility(View.VISIBLE);
            backViewTxt.setText(text);
        }
    }

    /**
     * 设置左边的文字的颜色
     *
     * @param color
     */
    public void setLeftTextColor(int color) {
        if (backViewTxt != null) {
            backViewTxt.setVisibility(View.VISIBLE);
            backViewTxt.setTextColor(color);
        }
    }

    /**
     * 设置左边的图片
     *
     * @param imgResouce
     */
    public void setLeftImg(int imgResouce) {
        if (backviewImg != null) {
            backviewImg.setVisibility(View.VISIBLE);
            backviewImg.setImageResource(imgResouce);
        }
    }

    /**
     * 设置右边的文字
     *
     * @param text
     */
    public void setRightText(String text, @ColorRes int color) {
        if (rightTxt != null) {
            rightTxt.setVisibility(View.VISIBLE);
            rightTxt.setText(text);
            rightTxt.setTextColor(getResources().getColor(color));
        }
    }


    /**
     * 设置右边的文字的颜色
     *
     * @param color
     */
    public void setRightTextColor(int color) {
        if (rightTxt != null) {
            rightTxt.setVisibility(View.VISIBLE);
            rightTxt.setTextColor(color);
        }
    }

    /**
     * 设置右边的图片
     *
     * @param imgResouce
     */
    public void setRightImg(int imgResouce) {
        if (rightviewImg != null) {
            rightviewImg.setVisibility(View.VISIBLE);
            rightviewImg.setImageResource(imgResouce);
        }
    }

    /**
     * 设置topview的背景色
     *
     * @param color
     */
    public void setTopViewBgColor(int color) {
        if (topViewLayout != null) {
            topViewLayout.setVisibility(View.VISIBLE);
            topViewLayout.setBackgroundColor(color);
        }
    }

    /**
     * 设置左边文字的点击事件
     *
     * @param listener
     */
    public void setLeftTextClick(OnClickListener listener) {
        if (backViewTxt != null) {
            backViewTxt.setOnClickListener(listener);
        }
    }

    /**
     * 设置左边图片的点击事件
     *
     * @param listener
     */
    public void setLeftImgClick(OnClickListener listener) {
        if (backviewImg != null) {
            backviewImg.setOnClickListener(listener);
        }
    }

    /**
     * 设置右边文字的点击事件
     *
     * @param listener
     */
    public void setRightTextClick(OnClickListener listener) {
        if (rightTxt != null) {
            rightTxt.setOnClickListener(listener);
        }
    }

    /**
     * 设置右边图片的点击事件
     *
     * @param listener
     */
    public void setRightImgClick(OnClickListener listener) {
        if (rightviewImg != null) {
            rightviewImg.setOnClickListener(listener);
        }
    }

    public ImageButton getBackviewImg() {
        return backviewImg;
    }

    public ImageButton getRightviewImg() {
        return rightviewImg;
    }

    public TextView getBackViewTxt() {
        return backViewTxt;
    }

    public TextView getRightTxt() {
        return rightTxt;
    }

    public void setBackground(int white_ff) {

    }
    public void setBgDra(int res){
        topViewLayout.setBackgroundResource(res);
    }




}
